package com.linked.universal.annotation.aspect;

import com.linked.universal.annotation.BeanTrim;
import com.linked.universal.utils.TUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author Bluerain
 * @version 1.0
 * @data 2025/1/8 14:40
 * @describe todo
 */
@Aspect
@Component
@Slf4j
public class BeanTrimAspect implements Ordered {


    // 此处为注解所在,根据实际设定
    @Before("@annotation(com.nhome.common.annotation.BeanTrim)")
    public void beforeMethod(JoinPoint point) throws Exception {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        BeanTrim syslog = method.getAnnotation(BeanTrim.class);
        Object[] args = point.getArgs();
        for (Object arg : args) {
            try {
                TUtils.trimBeanString(arg);
            } catch (Exception e) {
                throw new Exception("参数处理异常");
            }
        }

    }


    @Override
    public int getOrder() {
        return 1;
    }
}
